import csv
from selenium import webdriver

URL='http://jbk.39.net/bw_t1/'
#所有列表页面的URL
pageUrlList=[]
#列表页面的子页位置
itemXpathList=[]
#所有子页的URL
itemUrlList=[]
#并发症和常用药物
messageList=[]
#读取的csv地址
READ_ITEM_URL='itemUrl.csv'
#输出目录
WRITE_ITEM_URL='itemUrl.csv'
WRITE_MESSAGE_URL='message.csv'
class Main():
    def __init__(self):
        option = webdriver.ChromeOptions()
        option.add_argument('disable-infobars')
        self.driver = webdriver.Chrome(chrome_options=option)
        #self.driver.implicitly_wait(10)
        #self.driver.set_page_load_timeout(10)
        #self.driver.set_script_timeout(10)
        #print('>>>响应等待时间设置为10s')

    #创建所有页面的url
    def createpageUrl(self):
        print('>>>开始创建每页的url')
        #找到最大页数
        pageNumText=self.driver.find_element_by_xpath('//span[@class="res_page"]').text
        pageNum=''
        flag=0
        #（1/200）解析出最大数字
        for c in pageNumText:
            if c == '>':
                break
            if flag == 1:
                pageNum=pageNum+c
            if c == '/':
                flag=1
        #pageNum=1
        #先把第一页放入列表
        pageUrlList.append(URL)
        for num in range(1,(int(pageNum)+1)):
            #URL格式
            pageUrl = 'http://jbk.39.net/bw_t1_p' + str(num) + '#ps'
            pageUrlList.append(pageUrl)
            print(pageUrl)

    #创建子页xpath
    def createItemXPath(self):
        print('>>>开始创建ItemXPath')
        for num in range(1, 11):
            #Xpath格式
            itemXpath= '//div[@class="res lbox"]/div[2]/div[2]/div[' + str(num) + ']/a'
            itemXpathList.append(itemXpath)
            print(itemXpath)

    #获得每个子页的URL
    def getItemUrl(self):
        print('>>>开始获得子页URL')
        for pageUrl in pageUrlList:
            print('>>>pageUrl='+pageUrl)
            self.driver.get(pageUrl)
            for xpath in itemXpathList:
                try:
                    itemUrl=self.driver.find_element_by_xpath(xpath).get_attribute('href')
                    itemUrl=itemUrl[:-5]+'jbzs/'
                    list=[]
                    list.append(itemUrl)
                    itemUrlList.append(list)
                except:
                    print('跳过异常')
                    pass
                continue

    # 写入csv
    def writeCsv(self, list, writeUrl):
        #print('>>>开始写入csv')
        try:
            #a为追加模式
            with open(writeUrl, 'a', newline='',encoding='utf-8') as csvFile:
                writeFile = csv.writer(csvFile)
                writeFile.writerows(list)
        except IOError:
            print('ERROR:写入失败')
        #print('>>>写入完成')

    #检索所有疾病的并发症和药品
    def getMessage(self):
        print('>>>开始检索并发症和常用药品')
        index=0
        for itemUrl in itemUrlList:
            index=index+1
            itemUrl=itemUrl[0]
            try:
                self.driver.get(itemUrl)
            except:
                print('数据'+str(index)+'连接超时')
                continue
            #死循环，直到最后一条时退出
            message=[]
            strBfz=''
            strYp=''
            #名字
            try:
                name=self.driver.find_element_by_xpath('//div[@class="tit clearfix"]/a/h1').text
            except:
                print('异常：' + '//div[@class="tit clearfix"]/a/h1')
                continue
                pass
            #相关症状
            flag=1
            try:
                while(1):
                   title=self.driver.find_element_by_xpath('//dl[@class="info"][1]/dd['+str(flag)+']/i').text
                   if title == '相关症状：':
                        break
                   else:
                       flag = flag + 1
            except:
                print('异常：'+'//dl[@class="info"][1]/dd['+str(flag)+']/i')
                pass
            i=1
            try:
                while(1):
                    bfz=self.driver.find_element_by_xpath('//dl[@class="info"][1]/dd['+str(flag)+']/a['+str(i)+']').text
                    if bfz == '[详细]':
                        break
                    else:
                        if i == 1:
                            strBfz=strBfz+bfz
                        else:
                            strBfz=strBfz+'_'+bfz
                        i=i+1
            except:
                print('异常：'+'//dl[@class="info"][1]/dd['+str(flag)+']/a['+str(i)+']')
                pass
            #药物
            flag=1
            try:
                while(1):
                    title=self.driver.find_element_by_xpath('//dl[@class="info"][2]/dd['+str(flag)+']/i').text
                    if title == '常用药品：':
                        break
                    else:
                        flag = flag + 1
            except:
                print('异常：'+'//dl[@class="info"][2]/dd['+str(flag)+']/i')
                pass
            i=1
            try:
                while (1):
                    yp = self.driver.find_element_by_xpath( '//dl[@class="info"][2]/dd['+str(flag)+']/a['+str(i)+']').text
                    if yp == '[详细]':
                        break
                    else:
                        if i == 1:
                            strYp = strYp + yp
                        else:
                            strYp = strYp + '_' + yp
                        i = i + 1
            except:
                print('异常：'+'//dl[@class="info"][2]/dd['+str(flag)+']/a['+str(i)+']')
                pass
            message.append(name)
            message.append(strBfz)
            message.append(strYp)
            list=[]
            list.append(message)
            self.writeCsv(list,WRITE_MESSAGE_URL)
            #messageList.append(message)
            print('>>>数据'+str(index)+':'+message)

    #读取csv
    def readCsv(self,readToList,csvUrl):
        print('>>>开始读取csv')
        try:
            with open(READ_ITEM_URL, 'rt', encoding="utf-8") as csvFile:
                lines = csv.reader(csvFile)
                for line in lines:
                    list = []
                    list.append(line[0])
                    list.append(line[1])
                    readToList.append(list)
        except IOError:
            print('ERROR:读取道具表失败')



    def run(self):
        self.createItemXPath()
        #打开页面
        try:
            self.driver.get(URL)
        except:
            print('>>>ERROR:FAIL TO OPEN PAGE!')
        #创建所有页面的url
        self.createpageUrl()
        #读取itemUrl.csv
        #self.readCsv(itemUrlList,READ_ITEM_URL)
        #if len(itemUrlList)==0:
        #获得每个子页的URL
        self.getItemUrl()
        #把子页的URL输出到csv
        self.writeCsv(itemUrlList,WRITE_ITEM_URL)
        # 检索所有疾病的并发症和药品
        self.getMessage()
        #把结果输出到csv
        #self.writeCsv(messageList,WRITE_MESSAGE_URL)

# 直接运行这个py
if __name__ == "__main__":
    main = Main()
    main.run()